<?php

// Model:'ChiTietDoanKiemTra' - Database Table: 'ct_doan_tt'

Class ChiTietDoanKiemTra extends Eloquent {

    //Cac bang tham gia trong model
    private $tables = array("doan_tt", "ct_doan_tt", 'cb_tt', "chuc_vu", "chuc_danh", "vctdoantt");
    //Rang buoc du lieu
    private $rules = array(
        "ID_DOAN_TT" => "required",
        "ID_CB_TT" => "required",
        "ID_CV" => "required",
        "ID_CHUCDANH" => "required",
        "TRACH_NHIEM" => "min:1|max:500"
    );

    //Ham kiem tra du lieu
    public function validation($data) {
        return Validator::make($data, $this->rules);
    }

    //Lay ds du lieu
    public function GetList() {
        return DB::table($this->tables[5])
                ->select('*')
                ->first();
    }

    //Lay du lieu xac dinh
    public function GetOne($id) {
         return DB::table($this->tables[5])
                ->select('*')
                ->where($this->tables[0] . ".ID", $id)
                ->first();
    }

    public function GetOneWithMD($md) {

        return DB::table($this->tables[5])
                ->select('*')
                ->where($this->tables[0] . ".MA_DOAN_TT", $md)
                ->first();
    }

    //Luu du lieu moi
    public function SaveIn($data) {
        return DB::table($this->tables[1])
                        ->insert($data);
    }

    //Luu du lieu sua doi
    public function StoreIn($data, $id) {
        try {
            DB::table($this->tables[1])
                    ->where("ID", $id)
                    ->update($data);
            return true;
        } catch (Exception $ex) {
            return false;
        }
    }

    //Xoa du lieu xac dinh
    public function Remove($id) {
        try {
            return DB::table($this->tables[5])
                            ->delete($id);
        } catch (Exception $ex) {
            return false;
        }
    }

}
